package cn.mandifal.order.service.impl;


import cn.mandifal.mapper.DpItemMapper;
import cn.mandifal.mapper.DpOrderItemMapper;
import cn.mandifal.mapper.DpOrdersMapper;
import cn.mandifal.mapper.DpPayLogsMapper;
import cn.mandifal.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * redis 中 key 的过期时间监听
 * @author shun
 */
@Transactional
public class TopicMessageListener implements MessageListener {
    @Autowired
    private DpOrdersMapper dpOrdersMapper;
    @Autowired
    private DpOrderItemMapper dpOrderItemMapper;
    @Autowired
    private DpPayLogsMapper dpPayLogsMapper;
    @Autowired
    private DpItemMapper dpItemMapper;

    @Override
    public void onMessage(Message message, byte[] pattern) {
        // 请使用valueSerializer
        byte[] body = message.getBody();
        byte[] channel = message.getChannel();
        //设置监听频道
        String topic = new String(channel);
        //key
        String key = new String(body);
        //从失效key中筛选代表订单失效的key,
        if (key.contains("pay-")) {
            //截取订单号，查询订单，如果是未支付状态则取消订单
            int lastIndexOf = key.lastIndexOf("-");
            String payId = key.substring(lastIndexOf+1);
            //返还库存
            System.out.println("订单号为：" + payId + "的订单超时未支付，取消订单开始");
            //1.根据支付订单号得到支付日志实体
            DpPayLogs dpPayLogs = dpPayLogsMapper.selectByPrimaryKey(payId);
            //如果订单已支付则直接返回
            if (dpPayLogs.getTransactionId()!=null){
                return ;
            }
            //设置为超时状态
            dpPayLogs.setTradeState("8");
            //保存到数据库
            dpPayLogsMapper.updateByPrimaryKey(dpPayLogs);
            //2.修改订单表的订单状态,根据逗号截取，得到订单id列表
            String[] orderIdList = dpPayLogs.getOrderList().split(",");
            //遍历订单 id 列表，查询每个订单 id 下的订单详情
            for (String orderId:orderIdList) {
                //根据订单 id 得到订单实体
                DpOrders dpOrders = dpOrdersMapper.selectByPrimaryKey(orderId);
                if (dpOrders != null) {
                    dpOrders.setStatus("8");
                    dpOrders.setCloseTime(new Date());
                }
                //保存修改到数据库
                dpOrdersMapper.updateByPrimaryKey(dpOrders);

                //根据订单 id 得到订单详情
                DpOrderItemExample example = new DpOrderItemExample();
                DpOrderItemExample.Criteria criteria = example.createCriteria();
                criteria.andOrderIdEqualTo(orderId);
                List<DpOrderItem> list = dpOrderItemMapper.selectByExample(example);
                if (list.size() > 0) {
                    //遍历订单详情列表，修改其状态
                    for (DpOrderItem dpOrderItem : list) {
                        //设置订单详情状态为订单超时
                        dpOrderItem.setStatus("8");
                        //根据订单详情 id 查询商品 sku，
                        DpItem dpItem = dpItemMapper.selectByPrimaryKey(dpOrderItem.getItemId());
                        //设置库存为原有库存加商品sku购买量
                        dpItem.setNum(dpItem.getNum().intValue() + dpOrderItem.getNum().intValue());
                        //保存更改
                        dpItemMapper.updateByPrimaryKey(dpItem);
                        //保存修改
                        dpOrderItemMapper.updateByPrimaryKey(dpOrderItem);
                    }
                }
            }
            System.out.println("订单号为：" + payId + "的订单超时未支付，取消订单结束");

        }
    }


}